package _interview75;

/**
 * 1493. 删掉一个元素以后全为 1 的最长子数组
 */
public class No1493 {
    public int longestSubarray(int[] nums) {
        int k = 1;

        int answer = 0;

        for (int left = 0, right = 0; right < nums.length; ) {
            if (nums[right] == 1) {
                right++;
            } else if (k > 0) {
                right++;
                k--;
            } else if (nums[left++] == 0) k++;

            answer = Math.max(right - left, answer);
        }

        return answer - 1;
    }
}
